package cn.tedu.common.redis.config;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;


@Configuration
@ConfigurationProperties(prefix="shard.redis")
//@ConditionalOnClass({GenericObjectPoolConfig.class,Jedis.class,ShardedJedisPool.class})
public class ShardeJedisPoolConfig {
	//4个需要赋值的属性
	private List<String> nodes;//{"10.9.100.26:6379","",""}
	
	//创建连接池对象初始化
	@Bean
	public ShardedJedisPool initShardedJedisPool(){
		//收集节点信息
		List<JedisShardInfo> info=new ArrayList<JedisShardInfo>();
		for(String node:nodes){
			//node="10.9.100.26:6379"
			String host=node.split(":")[0];
			int port=Integer.parseInt(node.split(":")[1]);
			info.add(new JedisShardInfo(host,port));
		}
		//最大连接数,最大空闲
		GenericObjectPoolConfig config=new GenericObjectPoolConfig();
		config.setMaxIdle(10);
		config.setMinIdle(10);
		config.setMaxTotal(200);
		//构造连接池
		ShardedJedisPool pool=new ShardedJedisPool(config, info);
		return pool;
	}
	public List<String> getNodes() {
		return nodes;
	}
	public void setNodes(List<String> nodes) {
		this.nodes = nodes;
	}
}












